package com.zjl.litemall.db.service;

import com.github.pagehelper.PageHelper;
import com.zjl.litemall.db.dao.LitemallUserMapper;
import com.zjl.litemall.db.domain.LitemallUser;
import com.zjl.litemall.db.domain.LitemallUserExample;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;

/**
 * @author cat
 * @description
 * @date 2022/2/25 下午10:28
 */
@Service
public class LitemallUserService {

    @Resource
    private LitemallUserMapper userMapper;

    /**
     * @description 首页用户数量
     * @author Lemonade
     * @param:
     * @updateTime 2022/2/26 上午10:04
     * @return: int
     */
    public int count() {
        LitemallUserExample example = new LitemallUserExample();
        example.or().andDeletedEqualTo(false);

        return (int) userMapper.countByExample(example);
    }

    /**
     * @description 会员管理--查询
     * @author Lemonade
     * @param: username
     * @param: mobile
     * @param: page
     * @param: limit
     * @param: sort
     * @param: order
     * @updateTime 2022/2/26 下午2:27
     * @return: java.util.List<com.zjl.litemall.db.domain.LitemallUser>
     */
    public List<LitemallUser> querySelective(String username, String mobile,
                                             Integer page, Integer limit, String sort, String order){
        LitemallUserExample example = new LitemallUserExample();
        LitemallUserExample.Criteria criteria = example.createCriteria();

        if (!StringUtils.isEmpty(username)){
            criteria.andUsernameLike("%" + username + "");
        }
        if (!StringUtils.isEmpty(mobile)){
            criteria.andMobileEqualTo(mobile);
        }
        // 作用是？
        criteria.andDeletedEqualTo(false);

        if (StringUtils.isEmpty(sort)&&StringUtils.isEmpty(order)){
            example.setOrderByClause(sort + " " + order);
        }

        // 分页
        PageHelper.startPage(page,limit);
        return userMapper.selectByExample(example);
    }

    /**
     * @description
     * @author Lemonade
     * @param: user
     * @updateTime 2022/2/26 下午2:54
     * @return: int
     */
    public int updateById(LitemallUser user) {
        user.setUpdateTime(LocalDateTime.now());
        return userMapper.updateByPrimaryKeySelective(user);
    }

    /**
     * 根据主键ID查询
     * @param id
     * @return
     */
    public LitemallUser selectById(Integer id){
        return userMapper.selectByPrimaryKey(id);
    }
}
